
package com.kugou.game.sdk.core;

import com.baidu.location.BDLocation;
import com.kugou.game.sdk.entity.GiftBagTaoHaoResponse;
import com.kugou.game.sdk.entity.GiftbagListResponse;
import com.kugou.game.sdk.entity.GiftbagReceiveResponse;
import com.kugou.game.sdk.entity.GiftbagTakenListResponse;
import com.kugou.game.sdk.interfaces.OnRequestGameInfoListener;
import com.kugou.game.sdk.persistence.preference.PrefUtil;
import com.kugou.game.sdk.protocol.GameForumTask;
import com.kugou.game.sdk.protocol.GameForumTask.AppInfoHttpResponse;
import com.kugou.game.sdk.protocol.GiftGetCodeHttpTask;
import com.kugou.game.sdk.protocol.GiftReceiveHttpTask;
import com.kugou.game.sdk.protocol.RequestGameRankListTask;
import com.kugou.game.sdk.protocol.RequestGameRankListTask.RankListResponse;
import com.kugou.game.sdk.protocol.RequestGiftBagTakenListTask;
import com.kugou.game.sdk.protocol.RequestGiftbagTotalListTask;

public class GameManager {

    private final static String msgNetWorkAbnormal = "网络异常，请检查网络连接！";

    /**
     * 获取游戏论坛地址
     * 
     * @param listener
     */
    public static void requestGameInfo(OnRequestGameInfoListener listener) {
        if (listener == null) {
            throw new RuntimeException("OnRequestGameInfoListener can't be null");
        }
        GameForumTask httpTask = new GameForumTask();
        AppInfoHttpResponse response = httpTask.request();
        if (response != null && response.isOk()) {
            if (response.isUseful()) {
                listener.onSuccess(response.getForumUrl());
            } else {
                listener.onFailed(response.getPrompt());
            }
        } else {
            listener.onFailed(msgNetWorkAbnormal);
        }
    }

    /**
     * 请求游戏排行榜数据
     * 
     * @param location
     * @param score
     * @param account
     * @param role
     * @param gameid
     * @return
     */
    public static String requestGameRankList(BDLocation location, int score, String account,
            String role) {
        RequestGameRankListTask task = new RequestGameRankListTask();
        String province = location.getProvince();
        String city = location.getCity();
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        RankListResponse response = task.request(province, city, longitude, latitude, score,
                account, role);
        if (response != null) {
            return response.getRankListInfo();
        }
        return "网络连接异常！";

    }

    /**
     * 获取全部礼包列表
     * 
     * @param page
     * @param pageSize
     * @param merchantid
     * @param appid
     * @param appKey
     * @param gameId
     * @param nickname
     */
    public static GiftbagListResponse requestTotalGiftBagList(int page, int pageSize,
            String nickname) {

        RequestGiftbagTotalListTask task = new RequestGiftbagTotalListTask();
        GiftbagListResponse response = task.request(page, pageSize, nickname);
        return response;

    }

    /**
     * 获取已领取的礼包列表
     * 
     * @param page
     * @param pageSize
     * @param merchantid
     * @param appid
     * @param appKey
     * @param gameId
     * @param nickname
     */
    public static GiftbagTakenListResponse requestTakenGiftBagList(int page, int pageSize,
            String nickname) {

        RequestGiftBagTakenListTask task = new RequestGiftBagTakenListTask();
        GiftbagTakenListResponse response = task.request(page, pageSize, nickname);
        return response;
    }

    /**
     * 领取礼包
     * 
     * @param merchantId
     * @param appId
     * @param appKey
     * @param giftId
     * @param userName
     */
    public static GiftbagReceiveResponse requestGiftBag(long giftId, String nickname) {

        GiftReceiveHttpTask task = new GiftReceiveHttpTask();
        GiftbagReceiveResponse response = task.request(giftId, nickname);
        return response;

    }

    /**
     * 淘号
     * 
     * @param merchantId
     * @param appId
     * @param appKey
     * @param giftId
     * @param nickname
     * @param listener
     */
    public static GiftBagTaoHaoResponse taoGiftBag(long giftId, String nickname) {

        GiftGetCodeHttpTask task = new GiftGetCodeHttpTask();
        GiftBagTaoHaoResponse response = task.request(giftId, nickname);
        return response;

    }

    /**
     * 当前应用是否首次启动
     * 
     * @return 1是，0否
     */
    public static int isAppFirstStart() {
        boolean firstStart = PrefUtil.getAppFirstStart(SDKControler.getContext());
        if (firstStart) {
            PrefUtil.setAppFirstStart(SDKControler.getContext(), false);
            return 1;
        }
        return 0;
    }
}
